/**
 * @Description WxDeliveryAddressController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2017年11月28日 下午2:18:09
 */
package com.solution.wx.my.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.DeliveryAddressEntity;
import com.solution.admin.customer.service.DeliveryAddressService;
import com.solution.admin.point.constants.PointConstants.IsYesNo;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.utils.string.StringUtil;

/***
 * 收货地址
 * 
 * @author xu_cc
 * @date 创建时间：2017年11月28日 下午2:18:27
 */
@RequestMapping("/wx/address")
@Controller
public class WxDeliveryAddressController extends BaseController {
	@Autowired
	DeliveryAddressService deliveryAddressService;

	/**
	 * 转到微信收货地址列表页面
	 * 
	 * @param request
	 * @param model
	 *            void
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:20:49
	 */
	@RequestMapping("/toWxAddressListPage")
	public String toWxAddressListPage(HttpServletRequest request, Model model) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("customerId", getCustomerId());
			List<DeliveryAddressEntity> addressList = deliveryAddressService.listDeliveryAddress(param);
			model.addAttribute("addressList", addressList);
			request.setAttribute("shoppingIds", request.getParameter("shoppingIds"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/wx/my/address/address_list";
	}

	/**
	 * 转到编辑、新建页面
	 * 
	 * @param request
	 * @param model
	 *            void
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:20:49
	 */
	@RequestMapping("/toWxAddressEditPage")
	public String toWxAddressEditPage(HttpServletRequest request, Model model, String addressId) {
		try {
			if (StringUtil.isNotNull(addressId)) {
				DeliveryAddressEntity address = deliveryAddressService.getDeliveryAddressById(Long.parseLong(addressId));
				model.addAttribute("address", address);
			} else {
				model.addAttribute("address", new DeliveryAddressEntity());
			}
			request.setAttribute("shoppingIds", request.getParameter("shoppingIds"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/wx/my/address/address_form";
	}

	/**
	 * 保存地址
	 * 
	 * @param request
	 * @param model
	 * @param address
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:46:54
	 */
	@RequestMapping("/saveAddress")
	public String saveAddress(HttpServletRequest request, Model model, DeliveryAddressEntity address, String city, String areaCodes) {
		try {
			if (null != address) {
				if(address.getIsDefault() == null){
					address.setIsDefault(0);
				}
				address.setCustomerId(getCustomerId());
				address.setCompanyId(getCompanyId());
				if (null != address.getId()) {
					address.setModifier(getUserName());
					deliveryAddressService.updateDeliveryAddress(address);
				} else {
					address.setCreater(getUserName());
					address.setModifier(getUserName());
					deliveryAddressService.saveDeliveryAddress(address);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/wx/address/toWxAddressListPage?shoppingIds="+request.getParameter("shoppingIds");
	}

	/**
	 * 重设默认地址
	 * 
	 * @param request
	 * @param addressId
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:37:00
	 */
	@RequestMapping("/doSetDefault")
	@ResponseBody
	public BaseReturn doSetDefault(HttpServletRequest request, String addressId) {
		BaseReturn result = new BaseReturn();
		try {
			if (StringUtil.isNotNull(addressId)) {
				Long addressIdLong = Long.parseLong(addressId);
//				// 获取用户当前的默认地址
//				Map<String, Object> param = new HashMap<String, Object>();
//				param.put("customerId", getCustomerId());
//				param.put("isDefault", IsYesNo.YES);
//				List<DeliveryAddressEntity> addressList = deliveryAddressService.listDeliveryAddress(param);
//				if (null != addressList && addressList.size() > 0) {
//					for (DeliveryAddressEntity address : addressList) {
//						address.setIsDefault(Integer.parseInt(IsYesNo.NO));
//						address.setModifier(getUserName());
//						deliveryAddressService.updateDeliveryAddress(address);
//					}
//				}
				// 设置指定地址为默认地址
				DeliveryAddressEntity address = new DeliveryAddressEntity();
				address.setId(addressIdLong);
				address.setIsDefault(Integer.parseInt(IsYesNo.YES));
				address.setCustomerId(getCustomerId());
				address.setModifier(getUserName());
				int rows = deliveryAddressService.setDefaultAddress(address);
				if (rows > 0) {
					result.setCode(SysCode.SUCCESS);
					result.setMsg("设置成功~");
				} else {
					result.setCode(SysCode.ERROR);
					result.setMsg("设置失败，请重试！");
				}
			} else {
				result.setCode(SysCode.ERROR);
				result.setMsg("设置失败，请重试！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("设置失败，请重试！");
		}
		return result;
	}

	/**
	 * 删除地址
	 * 
	 * @param request
	 * @param model
	 * @param addressId
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月28日 下午2:30:34
	 */
	@RequestMapping("/doDelAddress")
	public String doDelAddress(HttpServletRequest request, String addressId) {
		try {
			deliveryAddressService.deleteDeliveryAddressById(Long.parseLong(addressId));
			request.setAttribute("shoppingIds", request.getParameter("shoppingIds"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/wx/address/toWxAddressListPage";
	}

}
